package osami.bundle;


import java.util.Vector;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
//import osami.engine.OsamiEngine;
import osami.communication.OsamiIdentifier;

/**
 * Reference object which stores information about a service.
 */
public class OsamiServiceReference implements ServiceReference
{
	
	private Vector<OsamiIdentifier> iRoute = new Vector<OsamiIdentifier>();
	
	private String iName = null;
	private String iInterface = null;
	
	// bundle is always null for remote references
	private Bundle iBundle = null;
	
	public OsamiServiceReference()
	{
	}

	
	public OsamiServiceReference( OsamiServiceReference aAnother )
	{
		iRoute = new Vector<OsamiIdentifier>( aAnother.getRoute() );
		iName = aAnother.getName();
		iInterface = aAnother.getInterface();
		
		iBundle = aAnother.getBundle();
	}
	
	
	/**
	 * Constructs an OSAMI bundle reference for a local service from a OSGI service reference object
	 * 
	 * @param aID
	 * @param aServiceReference
	 */
	public OsamiServiceReference( OsamiIdentifier aID, ServiceReference aServiceReference, BundleContext aBundleContext )
	{
		iRoute.add( aID );
		
		iBundle = aServiceReference.getBundle();

		OsamiBundle service = (OsamiBundle)aBundleContext.getService( aServiceReference );
		Class<?> interfaces[] = service.getClass().getInterfaces();

		iName = service.getClass().getName();
		
		if ( interfaces.length > 0 )
			iInterface = interfaces[0].getName();
	}
	
	/**
	 * Returns the name of the service class
	 * 
	 * @return String
	 */
	public String getName()
	{
		return iName;
	}

	/**
	 * Sets name for reference.
	 * 
	 * @param aName
	 */
	public void setName( String aName )
	{
		iName = aName;
	}
	
	/**
	 * Sets interface for reference.
	 * 
	 * @param aInterface
	 */
	public void setInterface( String aInterface )
	{
		iInterface = aInterface;
	}
	
	/**
	 * returns true if the route to the service goes through given node
	 * 
	 * @param aIdentifier
	 * @return boolean
	 */
	public boolean routeHas( OsamiIdentifier aIdentifier )
	{
		for ( int i = 0; i < iRoute.size(); i++ )
			if ( iRoute.elementAt(i).getID() == aIdentifier.getID() )
				return true;
		
		return false;
	}
	
	public Vector<OsamiIdentifier> getRoute()
	{
		return iRoute;
	}

	public void setRoute( Vector<OsamiIdentifier> aRoute )
	{
		iRoute = aRoute;
	}
	
	public String getInterface()
	{
		return iInterface;
	}
	
	public boolean implementsInterface( String aInterfaceName )
	{
		return iInterface.contentEquals( aInterfaceName );
	}
	
	public boolean equals( OsamiServiceReference aReference )
	{
		return (aReference.getName().equals( iName ) && aReference.getInterface().equals( iInterface ));
	}
	
	public boolean isLocal()
	{
		//if ( iRoute.elementAt(0).getID() == OsamiEngine.getInstance().getIdentifier().getID() ) return true;
		
		return false;
	}
	
	public Bundle getBundle()
	{
		return iBundle;
	}


	public int compareTo(Object reference) {
		// TODO Auto-generated method stub
		return 0;
	}


	public Object getProperty(String key) {
		// TODO Auto-generated method stub
		return null;
	}


	public String[] getPropertyKeys() {
		// TODO Auto-generated method stub
		return null;
	}


	public Bundle[] getUsingBundles() {
		// TODO Auto-generated method stub
		return null;
	}


	public boolean isAssignableTo(Bundle bundle, String className) {
		// TODO Auto-generated method stub
		return false;
	}
}
